home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZMemoryHeap.h < prev    next >
Text File  |  1997-08-16  |  5KB  |  197 lines

  1. /*
  2.  *  File:       ZMemoryHeap.h
  3.  *  Summary:    A class that uses a TAllocator and zero or more TFixedAllocators
  4.  *                to allocate blocks of memory.
  5.  *  Written by: Jesse Jones
  6.  *
  7.  *  Copyright ゥ 1997 Jesse Jones. 
  8.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  9.  *
  10.  *  Change History (most recent first):
  11.  *
  12.  *         <2>     8/15/97    JDJ        Block count functions are defined if !RELEASE (instead
  13.  *                                    of if DEBUG).
  14.  *         <1>     1/29/97    JDJ        Created
  15.  */
  16.  
  17. #pragma once
  18.  
  19. #include <Files.h>
  20. #include <New.h>
  21.  
  22. #include <ZTypes.h>
  23.  
  24.  
  25. //-----------------------------------
  26. //    Forward References
  27. //
  28. struct SMemoryBlock;
  29. class TAllocator;
  30. class TFixedAllocator;
  31.  
  32.  
  33. // ===================================================================================
  34. //    class TMemoryHeap
  35. //        This class is intended to be the principal way to allocate smallish blocks:
  36. //        the allocators are simply helper classes. Although TAllocator and TMemoryHeap
  37. //        have very similar interfaces I think the conceptual differences are large
  38. //        enough to make using a common base class questioanble. 
  39. // ===================================================================================
  40. class TMemoryHeap {
  41.  
  42.     friend class TDisableLeakChecking;
  43.  
  44. //-----------------------------------
  45. //    Initialization/Destruction
  46. //
  47. public:
  48.                         ~TMemoryHeap();
  49.  
  50.                         TMemoryHeap(TAllocator* takeAllocator);
  51.                         
  52. private:
  53.                         TMemoryHeap(const TMemoryHeap& rhs);
  54.                         
  55.             TMemoryHeap& operator=(const TMemoryHeap& rhs);
  56.                 
  57. //-----------------------------------
  58. //    Allocations
  59. //
  60. public:
  61.             void*        Allocate(ulong bytes);
  62.                         // Returns nil if allocation failed.
  63.                         
  64.             void         Deallocate(void* block);
  65.     
  66. //-----------------------------------
  67. //    Fixed Size Allocators
  68. //
  69. public:
  70.             void         AddAllocator(ulong blockSize, ulong maxBlocks);
  71.             
  72.             const TFixedAllocator* GetAllocator(ulong size) const;
  73.  
  74. //-----------------------------------
  75. //    Info
  76. //
  77. public:
  78.     // ----- Heap -----
  79.             ulong         GetHeapSize() const;
  80.             
  81.             ulong         GetPoolCount() const;
  82.                         // Returns the number of pools allocated (after the initial pool).
  83.                         // To avoid Memory Manager fragmentation this should be as small
  84.                         // as possible.
  85.                         
  86.             ulong         GetCurrentBytes() const                            {return mCurrentBytes;}
  87.                         // Returns the current number of bytes allocated in the heap.
  88.                         
  89.             ulong         GetHighwaterMark() const                        {return mHighwater;}
  90.                         // Returns the largest number of bytes ever allocated by the heap.
  91.                                     
  92.             ulong         GetDebugOverhead() const                        {return 100*mDebugOverhead/mCurrentBytes;}
  93.                         // Returns the percent of the current number of allocated bytes used
  94.                         // by debugging fields.
  95.  
  96.     // ----- Blocks -----
  97. #if !RELEASE
  98.             ulong         GetBlockCount() const                            {return mBlockCount;}
  99.                         // Returns the current number of allocated blocks.
  100.                         
  101.             ulong         GetTotalBlockCount(ulong size) const;
  102.                         // Returns the number of blocks that were ever allocated with the given size.
  103.  
  104.             ulong         GetCurrentBlockCount(ulong size) const;
  105.                         // Returns the number of currently allocated blocks with the given size.
  106.                         
  107.             void         DumpCommonBlocks();
  108.                         // Displays an alert containing statistics on the most commonly allocated 
  109.                         // block sizes.
  110.  
  111.             void         DumpAllocatorCapacities();
  112.                         // Displays an alert containing statistics on the fixed allocator capacities.
  113. #endif
  114.                         
  115.             ulong         GetBlockSize(const void* ptr) const;
  116.                         // Note that this may be slightly larger than the size passed to
  117.                         // Allocate.
  118.  
  119. //-----------------------------------
  120. //    Debugging
  121. //
  122. public:
  123. #if DEBUG
  124.     // ----- Zapping -----
  125.             bool         IsZapping() const;
  126.                         // Returns true if new and deleted blocks are being zapped.
  127.                         
  128.             void         SetZap(bool zap);
  129.                         // Zapping defaults to on if DEBUG is true.
  130.  
  131.     // ----- Validation -----
  132.             void         ValidateBlock(const void* ptr) const;
  133.             
  134.              void         ValidateHeap() const;
  135.  
  136.     // ----- Leak Checking -----
  137.             bool         IsLeakChecking() const;
  138.             
  139.             bool         IsLeakChecked(const void* ptr) const;
  140.             
  141.             ulong         GetLeakCount() const                            {return mLeakCount;}
  142.             
  143.             void         DumpLeaks() const;
  144. #endif
  145.  
  146. //-----------------------------------
  147. //    Internal API
  148. //
  149. public:
  150.     static    void*         operator new(size_t size);
  151.                         // Memory heaps can be used by the global operator new so we'll
  152.                         // take care of allocating ourselves.
  153.  
  154.     static    void         operator delete(void* ptr);
  155.     
  156. protected:
  157.             ulong         GetTotalBlockSize(const void* ptr) const;
  158.             
  159. #if DEBUG
  160.             void         AddDebugInfo(SMemoryBlock* block, ulong actualBytes, ulong size);
  161.             
  162.             FSSpec         GetLeaksLog() const;
  163. #endif
  164.     
  165. //-----------------------------------
  166. //    Member Data
  167. //
  168. protected:
  169.     TAllocator*            mAllocator;
  170.     TFixedAllocator*    mFixedAllocators[256];
  171.  
  172.     ulong                mBlockCount;
  173.     ulong                mLeakCount;
  174.     ulong                mHighwater;
  175.     ulong                mCurrentBytes;
  176.     ulong                 mDebugOverhead;
  177.         
  178. #if DEBUG
  179.     bool                mZap;
  180.     long                mLeakChecking;
  181. #endif
  182.  
  183. #if !RELEASE
  184.     ulong                 mTotalBlockCounts[256];
  185.     ulong                 mCurrentBlockCounts[256];
  186. #endif
  187. };
  188.  
  189.  
  190. // ===================================================================================
  191. //    Inlines
  192. // ===================================================================================
  193. #if DEBUG
  194.     inline bool TMemoryHeap::IsZapping() const            {return mZap;}
  195.     inline void TMemoryHeap::SetZap(bool zap)            {mZap = zap;}
  196.     inline bool TMemoryHeap::IsLeakChecking() const        {return mLeakChecking > 0;}
  197. #endif